﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace eTerm.NetSocket
{
    public class SocketPacket
    {
        BinaryWriter __bw;
        BinaryReader __br;
        private byte[] __OriginalBytes;

        #region 属性定义
        /// <summary>
        /// 数据处理流.
        /// </summary>
        /// <value>The packet stream.</value>
        protected MemoryStream PacketStream { get; set; }

        /// <summary>
        /// 指令序号.
        /// <remarks>
        ///     为线程安全自动递增
        /// </remarks>
        /// </summary>
        /// <value>The sequence.</value>
        public long Sequence { get; set; }
        #endregion

        #region 数据取
        /// <summary>
        /// Gets the stream.
        /// </summary>
        /// <returns></returns>
        public byte[] GetStream() {
            return PacketStream.ToArray();
        }
        #endregion

        #region 读取
        /// <summary>
        /// 压入Byte数组,并将流中当前位置提升数组长度
        /// </summary>
        /// <param name="value">字节数组</param>
        /// <param name="index">value中从零开始的字节偏移量</param>
        /// <param name="count">要写入当前流的字节数</param>
        public void Put(Byte[] value, int index, int count)
        {
            __bw.Write(value, index, count);
        }

        /// <summary>
        /// 写入单字节.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Put(byte value)
        {
            __bw.Write(value);
        }

        /// <summary>
        /// 写入字符.
        /// </summary>
        /// <param name="value">The value.</param>
        public void Put(char value)
        {
            __bw.Write(value);
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// Initializes a new instance of the <see cref="_Packet&lt;TSession&gt;"/> class.
        /// </summary>
        public SocketPacket()
        {
            PacketStream = new MemoryStream();
            __bw = new BinaryWriter(PacketStream);
            __br = new BinaryReader(PacketStream);
        }
        #endregion
    }
}
